package com.test.task.controller;

import com.test.task.beans.User;
import com.test.task.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLOutput;
import java.util.List;

/**
 * @DATE 2021/6/21 - 16:11
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/insertStaff")
    public String insertStaff(User user, Model model){
        Integer rows = adminService.addStaff(user);
        System.out.println(user);
        if(rows>0){
            return "redirect:/admin/queryAllStaff";
        }else{
            model.addAttribute("error","添加失败");
            return "/fail";
        }
    }
    @RequestMapping("/queryAllStaff")
    public String queryStaff(Model model){
        List<User> list = adminService.queryAllStaff();
        model.addAttribute("list",list);
        return "/admin/queryStaff";
    }
    @RequestMapping("/findStaffByUsername")
    public String findStaffByUsername(Model model,String username){
        User staff = adminService.findStaffByUsername(username);
        model.addAttribute("staff",staff);
        return "/admin/lookStaff";
    }
    @RequestMapping("/deleteStaffByUsername")
    public String deleteStaffByUsername(Model model,String username){
        Integer rows = adminService.deleteStaffByUsername(username);
        if(rows>0){
            return "redirect:/admin/queryAllStaff";
        }else{
            model.addAttribute("error","添加失败");
            return "/fail";
        }
    }

    @RequestMapping("/findNotAllocatedStaff")
    public String findNotAllocatedStaff(Model model){
        List<User> list = adminService.findNotAllocatedStaff();
        model.addAttribute("list",list);
        return "/admin/notAllocatedDept";

    }

    @RequestMapping("/findAllocatedDeptByUsername")
    public String findAllocatedDeptByUsername(Model model,String username){
        System.out.println("员工username:"+username);
        User staff = adminService.findStaffByUsername(username);
        List<User> list = adminService.findAllManager();
        model.addAttribute("staff",staff);
        model.addAttribute("list",list);
        return "/admin/allocatedDept";

    }
    @RequestMapping("/allocatedDeptByUsername")
    public String allocatedDeptByUsername(Model model,User user){
        System.out.println("分配给主管的员工:"+user);
       Integer rows = adminService.allocatedDeptByUsername(user);
        return "redirect:/admin/findNotAllocatedStaff";

    }



}
